package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.PartsType;
import com.sl.au.service.PartsTypeService;

@RestController
@RequestMapping(value = "/ui/partsType")
public class PartsTypeController extends BaseController {

	
	@Autowired
	PartsTypeService partsTypeService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("partsType/partsType");
		return modelAndView;
	}

	/**
	 * @param request
	 * @return 得到选项配置PartsType的map，关键字为key
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		if(key == null) key = "";
		List<PartsType> partsTypes = null;
		partsTypes = partsTypeService.getPage(start, length, key);
		Long count = null;
		count = partsTypeService.getCount(key);
		map.put("data", partsTypes);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getPartsType", method = RequestMethod.GET)
	public Map<String, Object> getPartsType(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<PartsType> partsTypes = null;
		String key = "";
		partsTypes = partsTypeService.getAllPartsType(key);
		map.put("data", partsTypes);
		return map;
	}
	///保存
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public Map<String, Object> savePartsType(HttpServletRequest request, @RequestBody PartsType partsType) {
		Map<String, Object> map = new HashMap<String, Object>();
		partsTypeService.getRepository().save(partsType);
		map.put("code", 0);
		return map;
	}
	
}
